// pages/NewWorkImform/NewWorkImform.js
Page({
  data: {
    maxWord: 1024,
    currentWord: 0,
    contentInput:'',
    WorkTitle:'',
    Description:'',
    WorkString:null, 
    // type:0,
    id:0,
    isListen:false,
    isRecite:false
  },

  writetitle(e){
   this.setData({
   WorkTitle:e.detail.value
   });
  },

  writedes(e){
  this.setData({
    Description:e.detail.value
  });
  },

// 解析输入字符
  limitWord: function (e) {
    var that = this;
    var value = e.detail.value;
    var wordLength = parseInt(value.length); //解析字符串长度转换成整数。
    if (that.data.maxWord < wordLength) {
      return;
    }
    that.setData({
      currentWord: wordLength,
      contentInput:e.detail.value
    });
  },
  /*图片浏览及上传 */
  browse: function () {
    var that=this;
    // if(this.data.type===0){
    //   that.tip('请选择作业类型')
    //     return false
    // }
    wx.chooseImage({
      count: 1,
      success:function(res){
        var tempFilePaths=res.tempFilePaths
        that.setData({
          picUrl:tempFilePaths[0],
          haveapicture:false
        })
        console.log(that.data.haveapicture)
        // var type=that.data.type;
        // console.log(type)
        //  跳转页面编辑图片
        wx.navigateTo({
          url: '/pages/piccut/piccut?type=1'+'&src='+tempFilePaths[0],
        })
        // 上传文件方法
      //   if(type==1){
      //     that.toTextListen(tempFilePaths[0])
      //     console.log("111");
      //   }
      //  else if(type==2){
      //    that.toTextRecite(tempFilePaths[0])
      //   }
      }
    })
  },

	format : function (dat) {
		 var year = dat.getFullYear();
    var mon = (dat.getMonth()+1) < 10 ? "0"+(dat.getMonth()+1) : dat.getMonth()+1;
    var data = dat.getDate()  < 10 ? "0"+(dat.getDate()) : dat.getDate();
    var hour = dat.getHours()  < 10 ? "0"+(dat.getHours()) : dat.getHours();
    var min =  dat.getMinutes()  < 10 ? "0"+(dat.getMinutes()) : dat.getMinutes();
    var seon = dat.getSeconds() < 10 ? "0"+(dat.getSeconds()) : dat.getSeconds();
                 
    var newDate = year +"-"+ mon +"-"+ data +" "+ hour +":"+ min +":"+ seon;
    return newDate;
	},

  // 布置作业
  submmit:function(){
    var that=this;
    let content = this.data.contentInput
    console.log(content)
    if(this.data.WorkTitle===""){
      that.tip('请输入作业标题')
        return false
    }
    else if(this.data.description===""){
      that.tip('请输入作业描述')
        return false
    }
    // else if(this.data.type===0){
    //   that.tip('请选择作业类型')
    //     return false
    // }
    else if (content==='') {
      that.tip( '请输入作业内容')
        return false
    }
    else {
      let arr=content.trim().split(/[\s\n]/)
      let res=new Set()
      for(let i=0;i<arr.length;i++){
        let isOk=/^[a-zA-Z]+$/.test(arr[i])
        if(isOk){
          res.add(arr[i])
        }
      }
      let wordList=Array.from(res)
      if(wordList.length<=0){
        that.tip( '请输入英语单词，以空格或回车分割')
        return
      }
      that.tip('以为您自动过滤出有效的英文单词')
      content=wordList.join(' ')
      var phoneNumber=wx.getStorageSync('phone');
      console.log(phoneNumber);
      if(that.data.id==0){
        wx.request({
          // 2022/11/5
          url: 'http://106.15.67.178:8080/createWork/',
          data: {
            'tittle': that.data.WorkTitle,
            'description': that.data.Description,
            'content':content,
            'ticket':phoneNumber,
            // 'createTime':that.format(new Date)
            // 'type':that.data.type
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res);
            wx.showToast({
              title: '布置成功',
              icon: 'success',
              duration: 2000,
              success: function(res) {
                wx.switchTab({
                  url: '/pages/workList/workList',
                })
              }
              })
          },
          fail:function(abc){
            console.log("----调用失败----")
            console.log(abc)
          },
        })
      }
      else {
        console.log(phoneNumber);
      wx.request({
        url: 'http://106.15.67.178:8080/getWorklist/',
        data: {
          //2022.11.6
          // 'title': that.data. WorkTitle,
          // 'description': that.data.Description,
          // 'content':content,
          // 'type':that.data.type
          'ticket':phoneNumber
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000,
            success: function(res) {
              wx.switchTab({
                url: '/pages/workList/workList',
              })
            }
            })
        },
        fail:function(abc){
          console.log("----调用失败----")
          console.log(abc)
        },
      })}
      }
    },
  // 帮助说明
  help:function(){
    wx.showModal({
      title: '帮助',
      content: '背诵作业：\r\n只需把课文内容录入即可，内容长度不超过1024个字，若超过请对课文内容进行分割 \r\n听力作业：\r\n每个词组之间使用空格隔开，程序将在每个词组之间插入停顿。若单个词组长度超过256个字，该词组将不会进行语音播报',
      showCancel:false,
      success: function (res) {
        if (res.confirm) {//这里是点击了确定以后
          console.log('用户点击确定')
        } 
      }
    })
  },
  tip(e){
    wx.showModal({
      title: '提示',
      content:e,
      showCancel:false
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(JSON.stringify(options)!="{}"){
    this.setData({
      id: options.id,
      Description:options.description,
      WorkTitle:options.title,
      contentInput: options.content,
      // type:options.type
    })
    // console.log(this.data.type)
    if(this.data.id>0){
    // var state=this.data.type;
    // if(state>1){
    //   this.setData ({
    //     isRecite:true
    //   })
    // }
    // else {
    //   this.setData ({
    //     isListen:true
    //   })
    // }
    }
   } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },



})